package auditoryUIbk;
import jass.render.*;
import jass.engine.*;
import jass.generators.*;
import java.util.*;

public class SoundManager {
  
    // JASS configuration
    static String aAPI = "javasound";
    static float srate = 22050;
    static int bufferSize = 512;
    static int bufferSizeRender = 2048;
  
    // 3D parameters for the eight directions
    // {leftVolume, rightVolume, leftDelay, rightDelay}
    static float V1 = 0.6f;
    static float V2 = 0.5f;
    static float V3 = 0.4f;
    static float V4 = 0.2f;
    static float D1 = 0.0002f;
    static float D2 = 0.001f;
    static float[][] params3D = new float[][] {
        {V2, V2,  0,  0 },
        {V3, V1,  D1, 0 },
        {V4, 1,   D2, 0 },
        {V3, V1,  D1, 0 },
        {V2, V2,  0,  0 },
        {V1, V3,  0,  D1},
        {1,  V4,  0,  D2},
        {V1, V3,  0,  D1}
    };
    
    // The JASS global sound player
    static SourcePlayer player;
    
    public static void configure(String aAPI_, float srate_, int bufferSize_, int bufferSizeRender_) {
        aAPI = aAPI_;
        srate = srate_;
        bufferSize = bufferSize_;
        bufferSizeRender = bufferSizeRender_;
        
        // Create JASS player
        double latencyInSeconds = (bufferSize*bufferSizeRender)/srate;
        player = new SourcePlayer(bufferSize,bufferSizeRender,srate,"SB Audigy Audio [DF80");
        player.setNChannels(2);
        if(aAPI.equals("asio")) {
            player.setUseNativeSound(true,"ASIO"); // ASIO buffer == synthesisBuffersize 
        } else if(aAPI.equals("directx")) {
            player.setUseNativeSound(true, "DirectX");
        } else if(aAPI.equals("rtaudio")) {
            player.setUseNativeSound(true);
            player.setNumRtAudioBuffersNative(bufferSizeRender);
        } else {
            player.setUseNativeSound(false);
            latencyInSeconds = (bufferSizeRender)/srate;
        }
//        System.out.println("Native Latency=  " + latencyInSeconds);

    } 
  
    public static void init() {
  
        
       if(player.getUseNativeSound() && player.getAudioAPI() == "ASIO") {
            player.initASIO();
       } else {
            player.start();
       }
    }
    
    public static void quit() {
        player.stopPlaying();
        try{
            Thread.currentThread().sleep(500);
        } catch(Exception e3) {
        }
    }
}